package com.demo.oauth.server.security.clientdetails;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Component;

import com.demo.oauth.server.model.ApplicationClient;
import com.demo.oauth.server.service.ApplicationClientService;

@Component
public class DemoClientDetailsService implements ClientDetailsService {

	@Autowired
	private ApplicationClientService applicationClientService;

	@Override
	public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
		ApplicationClient client = applicationClientService.selectById(clientId);
		BaseClientDetails details = new BaseClientDetails(client.getClientId(), client.getResourceIds(),
				client.getScope(), client.getGrantTypes(), client.getAuthorities(), client.getRedirectUri());
		details.setAutoApproveScopes(details.getScope());
		details.setAccessTokenValiditySeconds(client.getAccessTokenValidity());
		details.setRefreshTokenValiditySeconds(client.getRefreshTokenValidity());
		return details;
	}

}
